<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/2/2
 * Time: 下午10:39
 */

namespace Home\Controller;
use Think\Controller;

class ManageController extends Controller {

    //管理活首页
    public function index(){
        //获取有无未读消息
        $message_db = D("Message");
        $message_result = $message_db->getIsRead($_SESSION['loginUser']['user_id']);
        if(is_array($message_result)){
            session('is_read',1);
        }else{
            session('is_read',2);
        }
        session('entrance', 1);
        $this->assign('curNav', 1);
        $this->display();
    }

    //管理活首页
    public function indexPC(){
        $this->assign('curNav', 1);
        $this->theme('PC')->display('Fetcher/manage');
    }

    //管理活首页
    public function publisherPC(){
        $this->assign('curNav', 1);
        $this->theme('PC')->display('Publisher/manage');
    }

    //管理活首页－信息获取
    public function getManageInfo(){
        if(isset($_SESSION['loginUser'])){
            if(IS_GET){
                $publisher_id = $_SESSION['loginUser']['user_id'];
                $mission_db = D('Mission');
                $mission_result = $mission_db->getManageInfo($publisher_id);
                if(is_array($mission_result)){
                    $this->success('获取信息成功',$mission_result);
                }else{
                    $this->error('获取信息失败');
                }
            }else{
                $this->error('错误的方法');
            }
        }else{
            $this->error('请登录系统',U('Login/index'));
        }
    }

    //管理招募中的活1-index
    public function detail(){
        $mission_id = $_GET['mission'];
        if(isset($mission_id)) {
            session('entrance', 1);
            $this->assign('curNav', 1);
            $this->assign('mission_id', $mission_id);
            $this->display();
        } else {
            $this->error('获取任务信息失败');
        }
    }

    public function detailPC(){
        $mission_id = $_GET['mission'];
        if(isset($mission_id)) {
            $this->assign('mission_id', $mission_id);
            $this->theme('PC')->display('Mission/modify');
        } else {
            $this->error('获取任务信息失败');
        }
    }

    //管理招募中的活-信息获取
    public function getOneManageInfo(){
        if(isset($_SESSION['loginUser'])){
            if(IS_GET){
                $mission_id = $_GET['mission_id'];
                $mission_db = D('Mission');
                $mission_result = $mission_db->getOneManageInfo($mission_id);
                if(is_array($mission_result)){
                    $this->success('获取信息成功',$mission_result);
                    // dump($mission_result);
                }else{
                    $this->error('获取信息失败');
                }

            }
        }else{
            $this->error('请登录系统',U('Login/index'));
        }
    }

    //管理招募中的活－发布
    public function updateMission(){
        if(IS_POST){
            if( $_POST['mission_name'] && $_POST['jobs'] && $_POST['duration'] && $_POST['salary1'] && $_POST['location1'] && $_POST['valid_time'] && $_POST['isphone_public'] && $_POST['isagent'] ){
                $mission_id = $_POST['mission_id'];
                $data['mission_name'] = $_POST['mission_name'];
                $data['valid_time'] = $_POST['valid_time'];
                $data['isphone_public'] = $_POST['isphone_public'];
                $data['jobs'] = $_POST['jobs'];
                $data['duration'] = $_POST['duration'];
                $data['salary1'] = $_POST['salary1'];
                $data['salary2'] = $_POST['salary2'];
                $data['location1'] = $_POST['location1'];
                $data['location2'] = $_POST['location2'];
                $data['description'] = $_POST['description'];
                ini_set('date.timezone','Asia/Shanghai');
                $data['publish_time'] = date('Y-m-d H:i:s',time());

                if(2 == $_POST['isagent']){
                    $user_ext_db = D('UserExt');
                    $data['agent'] = $user_ext_db->getAgent($data['publisher_id']);
                }else{
                    $data['agent'] = '';
                }
                //设置状态
                $data['status'] = 1;
                //mission表存入信息
                $mission_db = M('Mission');
                $mission_result = $mission_db->where(array('mission_id'=>$mission_id))->save($data);

                if($mission_result){

                    $this->success("发布成功!");
                }else{
                    $this->error("发布失败!");
                }
            }else{
                $this->error('发布的内容不全');
            }

        }else{
            $this->error('错误方法');
        }
    }

    //管理招募中的活－结束
    public function endMission(){
        if(IS_POST){
            $mission_id = $_POST['mission_id'];
            //评价演员
            $miss_user_relate_db = D('MissUserRelate');
            $actor_info_arr = $_POST['actor_info'];
            //dump($actor_info_arr);
            foreach($actor_info_arr as $actor_info_arr_one){
                if($actor_info_arr_one['is_selected'] === true ){
                    $miss_user_relate_data['is_selected'] = 1;
                }else{

                    $miss_user_relate_data['is_selected'] = 3;
                }

                $miss_user_relate_map['mission_id'] = array('eq',$mission_id);
                $miss_user_relate_map['follower_id'] = array('eq',$actor_info_arr_one['user_id']);
                $miss_user_relate_data['evaluate'] = $actor_info_arr_one['evaluate'];
                $miss_user_relate_result = $miss_user_relate_db->where($miss_user_relate_map)->save($miss_user_relate_data);
                //dump($miss_user_relate_result);
            }


            //结束活
            $data['evaluate'] = $_POST['evaluate'];
            $data['status'] = 2;
            ini_set('date.timezone','Asia/Shanghai');
            $data['publish_time'] = date('Y-m-d H:i:s',time());
            $mission_db = M('Mission');
            $mission_result = $mission_db->where(array('mission_id'=>$mission_id))->save($data);
            if($mission_result){

                $this->success("结束成功!");
            }else{
                $this->error("结束失败!");
            }
        }else{
            $this->error('错误方法');
        }
    }

    //管理结束中的活－修改
    public function updateEndMission(){
        if(IS_POST){
            $mission_id = $_POST['mission_id'];
            $data['isphone_public'] = $_POST['isphone_public'];
            $data['status'] = 1;
            ini_set('date.timezone','Asia/Shanghai');
            $data['publish_time'] = date('Y-m-d H:i:s',time());
            $mission_db = M('Mission');
            $mission_result = $mission_db->where(array('mission_id'=>$mission_id))->save($data);
            if($mission_result){
                $this->success("修改成功!");
            }else{
                $this->error("修改失败!");
            }

        }else{
            $this->error('错误方法');
        }
    }

     //就要他了－获取项目列表
    public function getProjectList(){
        if(IS_GET){
            $publisher_id = $_SESSION['loginUser']['user_id'];
            $mission_db = D('Mission');
            $mission_result = $mission_db->getProjectList($publisher_id);
            if($mission_result){
                $this->success('获取项目列表成功',$mission_result);
            }else{
                $this->error('获取项目列表失败');
            }
        }else{
            $this->error('错误方法');
        }
    }



      //就要他了－ajax提交
       public function isSelected(){
            if(IS_POST){
                if(isset($_POST['follower_id']) && isset($_POST['mission_id'])){
                    //发消息
                    $message_db = M("Message");
                    $message_data['publisher_id'] = $_SESSION['loginUser']['user_id'];
                    $message_data['receiver_id'] = $_POST['follower_id'];
                    $content = "你好，你已通过审核！";
                    $mission_db = M('Mission');
                    $mission_result = $mission_db->getByMission_id($_POST['mission_id']);
                    $mission_name = $mission_result['mission_name'];
                    $url = U('Task/index').'/mission/'.$_POST['mission_id'];
                    $message_data['content'] = $content."!@#$%^&*".$mission_name."!@#$%^&*".$url;
                    $message_data['is_read'] = 1;
                    ini_set('date.timezone','Asia/Shanghai');
                    $message_data['time'] = date('Y-m-d H:i:s',time());
                    $message_result = $message_db->add($message_data);

                    //发送短信通知
                    $user_db = M("User");
                    $user_result = $user_db->getByUser_id($message_data['receiver_id']);
                    $phone = $user_result['phone'];
                    //$sendContent = '你好，你已通过《'.$mission_name.'》项目的审核 ,请登陆活控查看具体信息！';
                    $url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
                    $post_data = "account=cf_gwhz&password=XLyxR3&mobile=".$phone."&content=".rawurlencode("您好，您已通过《".$mission_name."》项目的审核，请及时登录活控（ihuokong.com）查看具体消息。");
                    $result = xml_to_array(post($post_data,$url));
                    //dump($result);

                    //建立或者改变miss_user_relate表记录
                    $data['mission_id'] = $_POST['mission_id'];
                    $data['follower_id'] = $_POST['follower_id'];
                    $data['is_selected'] = 2;
                    $miss_user_relate_db = D('MissUserRelate');
                    $miss_user_relate_map['mission_id'] = array('eq',$data['mission_id']);
                    $miss_user_relate_map['follower_id'] = array('eq',$data['follower_id']);
                    $miss_user_relate_exist = $miss_user_relate_db->isExist($data['mission_id'],$data['follower_id']);
                    //dump($miss_user_relate_exist);
                    if($miss_user_relate_exist){
                        $miss_user_relate_result = $miss_user_relate_db->where($miss_user_relate_map)->save($data);
                    }else{
                        $miss_user_relate_result = $miss_user_relate_db->add($data);
                    }
                    if($miss_user_relate_result){
                        $this->success('操作成功',$miss_user_relate_result);
                    }else{
                        $this->error('操作失败');
                    }
                }else{
                    $this->error('参数不全');
                }

            }else{
                $this->error('错误方法');
            }
       }


    //完成评价
    public function evaluateReceiver(){
        if(IS_POST){
            $evaluate = $_POST['evaluate'];
            if( NULL != $evaluate ){
                $data['evaluate'] = $evaluate ;
            }else{
                $data['evaluate'] = 0 ;
            }
            $miss_user_relate_map['mission_id'] = $_POST['mission_id'];
            $miss_user_relate_map['follower_id'] = $_POST['user_id'];
            $miss_user_relate_map['is_selected'] = 2;
            $miss_user_relate_db = M('MissUserRelate');
            $miss_user_relate_result = $miss_user_relate_db->where($miss_user_relate_map)->save($data);
            if($miss_user_relate_result){
                $this->success('评价成功');
            }else{
                $this->error('评价成功');
            }

        }else{
            $this->error('错误方法');
        }
    }

    //不满意，不要他
    public function evaluateNoReceive(){
        if(IS_POST){
//            $data['evaluate'] = $_POST['evaluate'];
            $data['is_selected'] = 1;
            $miss_user_relate_map['mission_id'] = $_POST['mission_id'];
            $miss_user_relate_map['follower_id'] = $_POST['user_id'];
            //$miss_user_relate_map['is_selected'] = 2;
            $miss_user_relate_db = M('MissUserRelate');
            $miss_user_relate_result = $miss_user_relate_db->where($miss_user_relate_map)->save($data);
            if($miss_user_relate_result){
                $this->success('评价成功');
            }else{
                $this->error('评价成功');
            }
        }else{
            $this->error('错误方法');
        }
    }

    //*************************************管理接的活******************************************//
    //

    //已接到的活儿跳转
    public function recvIndex(){
        session('entrance', 1);
        $this->assign('curNav', 1);
        $this->display('recv_index');
    }

    public function receivedMission(){
        if(IS_GET){
            $user_id = $_SESSION['loginUser']['user_id'];
            $miss_user_relate_db = D('MissUserRelate');
            $miss_user_relate_result = $miss_user_relate_db->getReceivedMission($user_id);
            if(is_array($miss_user_relate_result)){
                $this->success('获取信息成功',$miss_user_relate_result);
                // dump($miss_user_relate_result);
            }else{
                $this->error('获取信息失败');
            }
        }else{
            $this->error('错误方法');
        }
    }

    //已接到的活儿详情跳转
    public function recvDetail(){
        $mission_id = $_GET['mission'];
        if(isset($mission_id)) {
            session('entrance', 1);
            $this->assign('curNav', 1);
            $this->assign('mission_id', $mission_id);
            $this->display('recv_detail');
        } else {
            $this->error('获取任务信息失败');
        }
    }

    public function receivedOneMission(){
        if(IS_GET){
            $mission_id = $_GET['mission_id'];
            $mission_db = D('Mission');
            $mission_result = $mission_db->getOneManageInfo($mission_id);
            if(is_array($mission_result)){
                $this->success('获取信息成功',$mission_result);
                // dump($mission_result);
            }else{
                $this->error('获取信息失败');
            }
        }else{
            $this->error('错误方法');
        }
    }
    //***************************************************************************************//

//    //管理活单任务页面动态方法－修改项目名称
//    public function changeProjectName(){
//        if(IS_POST){
//            $mission_id = $_POST['mission_id'];
//            $data['mission_name'] = $_POST['mission_name'];
//            ini_set('date.timezone','Asia/Shanghai');
//            $data['publish_time'] = date('Y-m-d H:i:s',time());
//            $mission_db = M('Mission');
//            $mission_result = $mission_db->where(array('mission_id'=>$mission_id))->save($data);
//            if($mission_result){
//                $this->success('修改成功');
//            }else if( 0 == $mission_result ){
//                $this->error('项目名称未变');
//            }else{
//                $this->error('修改失败');
//            }
//        }else{
//            $this->error('错误方法');
//        }
//    }
//
//    //管理活单任务页面动态方法－修改项目岗位
//    public function changeJobs(){
//        if(IS_POST){
//            $mission_id = $_POST['mission_id'];
//            $data['jobs'] = $_POST['jobs'];
//            ini_set('date.timezone','Asia/Shanghai');
//            $data['publish_time'] = date('Y-m-d H:i:s',time());
//            $mission_db = M('Mission');
//            $mission_result = $mission_db->where(array('mission_id'=>$mission_id))->save($data);
//            if($mission_result){
//                $this->success('修改成功');
//            }else if( 0 == $mission_result ){
//                $this->error('项目工作未变');
//            }else{
//                $this->error('修改失败');
//            }
//        }else{
//            $this->error('错误方法');
//        }
//    }
//
//    //管理活单任务页面动态方法－修改有效期
//    public function changeValidTime(){
//        if(IS_POST){
//            $mission_id = $_POST['mission_id'];
//            $data['valid_time'] = $_POST['valid_time'];
//            ini_set('date.timezone','Asia/Shanghai');
//            $data['publish_time'] = date('Y-m-d H:i:s',time());
//            $mission_db = M('Mission');
//            $mission_result = $mission_db->where(array('mission_id'=>$mission_id))->save($data);
//            if($mission_result){
//                $this->success('修改成功');
//            }else if( 0 == $mission_result ){
//                $this->error('有效期未变');
//            }else{
//                $this->error('修改失败');
//            }
//        }else{
//            $this->error('错误方法');
//        }
//    }
//
//    //管理活单任务页面动态方法－发布活时公布电话
//    public function changeIsPhonePublic(){
//        if(IS_POST){
//            $mission_id = $_POST['mission_id'];
//            $data['isphone_public'] = $_POST['isphone_public'];
//            ini_set('date.timezone','Asia/Shanghai');
//            $data['publish_time'] = date('Y-m-d H:i:s',time());
//            $mission_db = M('Mission');
//            $mission_result = $mission_db->where(array('mission_id'=>$mission_id))->save($data);
//            if($mission_result){
//                $this->success('修改成功');
//            }else if( 0 == $mission_result ){
//                $this->error('是否公布电话未变');
//            }else{
//                $this->error('修改失败');
//            }
//        }else{
//            $this->error('错误方法');
//        }
//    }
//
//    //管理活单任务页面动态方法－修改工作时限
//    public function changeDuration(){
//        if(IS_POST){
//            $mission_id = $_POST['mission_id'];
//            $data['duration'] = $_POST['duration'];
//            ini_set('date.timezone','Asia/Shanghai');
//            $data['publish_time'] = date('Y-m-d H:i:s',time());
//            $mission_db = M('Mission');
//            $mission_result = $mission_db->where(array('mission_id'=>$mission_id))->save($data);
//            if($mission_result){
//                $this->success('修改成功');
//            }else if( 0 == $mission_result ){
//                $this->error('工作时限未变');
//            }else{
//                $this->error('修改失败');
//            }
//        }else{
//            $this->error('错误方法');
//        }
//    }
//
//    //管理活单任务页面动态方法－修改薪酬范围
//    public function changeSalary(){
//        if(IS_POST){
//            $mission_id = $_POST['mission_id'];
//            $data['salary1'] = $_POST['salary1'];
//            $data['salary2'] = $_POST['salary2'];
//            ini_set('date.timezone','Asia/Shanghai');
//            $data['publish_time'] = date('Y-m-d H:i:s',time());
//            $mission_db = M('Mission');
//            $mission_result = $mission_db->where(array('mission_id'=>$mission_id))->save($data);
//            if($mission_result){
//                $this->success('修改成功');
//            }else if( 0 == $mission_result ){
//                $this->error('薪酬未变');
//            }else{
//                $this->error('修改失败');
//            }
//        }else{
//            $this->error('错误方法');
//        }
//    }
//
//    //管理活单任务页面动态方法－修改工作地点
//    public function changeLocation(){
//        if(IS_POST){
//            $mission_id = $_POST['mission_id'];
//            $data['location1'] = $_POST['location1'];
//            $data['location2'] = $_POST['location2'];
//            ini_set('date.timezone','Asia/Shanghai');
//            $data['publish_time'] = date('Y-m-d H:i:s',time());
//            $mission_db = M('Mission');
//            $mission_result = $mission_db->where(array('mission_id'=>$mission_id))->save($data);
//            if($mission_result){
//                $this->success('修改成功');
//            }else if( 0 == $mission_result ){
//                $this->error('工作地点未变');
//            }else{
//                $this->error('修改失败');
//            }
//        }else{
//            $this->error('错误方法');
//        }
//    }
//
//    //管理活单任务页面动态方法－修改描述
//    public function changeDescription(){
//        if(IS_POST){
//            $mission_id = $_POST['mission_id'];
//            $data['jobs'] = $_POST['jobs'];
//            ini_set('date.timezone','Asia/Shanghai');
//            $data['publish_time'] = date('Y-m-d H:i:s',time());
//            $mission_db = M('Mission');
//            $mission_result = $mission_db->where(array('mission_id'=>$mission_id))->save($data);
//            if($mission_result){
//                $this->success('修改成功');
//            }else if( 0 == $mission_result ){
//                $this->error('项目工作未变');
//            }else{
//                $this->error('修改失败');
//            }
//        }else{
//            $this->error('错误方法');
//        }
//    }

    //test
    public function test(){
        $mission_id = $_GET['mission_id'];
        $mission_db = D('Mission');
        $mission_result = $mission_db->getOneManageInfo($mission_id);
        dump($mission_result);

//        $user_id = $_GET['user_id'];
//        $mission_db = D('MissUserRelate');
//        $mission_result = $mission_db->getReceivedMission($user_id);
//        dump($mission_result);
    }


} 